package com.yanqu.road.logic.activity.foxcard.game.sorcery;

import com.yanqu.road.entity.activity.foxcard.FoxBaseCard;
import com.yanqu.road.entity.activity.foxcard.FoxHandCard;
import com.yanqu.road.logic.activity.foxcard.FoxCardConfig;
import com.yanqu.road.logic.activity.foxcard.game.FoxCardGameLogic;
import com.yanqu.road.logic.activity.foxcard.game.FoxCardShowInfo;
import com.yanqu.road.logic.activity.foxcard.game.eFoxCardShowType;

import java.util.ArrayList;
import java.util.List;

/**
 * 将当前的手牌，全换点数或花色
 * 花色|点数|0|0|0
 */
public class FoxCardSorceryUse4 extends FoxCardSorceryUseBase {
    @Override
    protected boolean sorceryUse(FoxCardGameLogic gameLogic) {
        if (gameLogic.getGameData().getHandCards().isEmpty()) {
            return false;
        }

        FoxCardConfig config = gameLogic.getConfig();
        int color = sorceryConfig.getParamList().get(0);
        if (color == -1) {
            color = gameLogic.getConfig().getRandomHelper().next(1, FoxCardGameLogic.CARD_COLOR_NUM + 1);
        }
        int point = sorceryConfig.getParamList().get(1);
        if (point == -1) {
            point = gameLogic.getConfig().getRandomHelper().next(1, config.getPointToScoreMap().size() + 1);
        }

        if (color > 0) {
            List<FoxBaseCard> triggerCards = new ArrayList<>();
            for (FoxHandCard handCard : gameLogic.getGameData().getHandCards()) {
                if (config.isAllColorHandCard(handCard.getReinforceEffect())) {
                    continue;
                }
                if (config.isNotPointAndNotColorHandCard(handCard.getReinforceEffect())) {
                    continue;
                }
                if (handCard.getColor() != color) {
                    handCard.setColor(color);
                    triggerCards.add(handCard);
                }
            }
            if (triggerCards.isEmpty()) {
                return false;
            }
            FoxCardShowInfo showInfo = gameLogic.newFoxShow(eFoxCardShowType.COLOR_CHANGE.getValue());
            showInfo.setTriggerCards(triggerCards);
            showInfo.setShowValue(color);
        }

        if (point > 0) {
            List<FoxBaseCard> triggerCards = new ArrayList<>();
            for (FoxHandCard handCard : gameLogic.getGameData().getHandCards()) {
                if (config.isNotPointAndNotColorHandCard(handCard.getReinforceEffect())) {
                    continue;
                }
                if (handCard.getPoint() != point) {
                    handCard.setPoint(point);
                    triggerCards.add(handCard);
                }
            }
            if (triggerCards.isEmpty()) {
                return false;
            }
            FoxCardShowInfo showInfo = gameLogic.newFoxShow(eFoxCardShowType.POINT_CHANGE.getValue());
            showInfo.setTriggerCards(triggerCards);
            showInfo.setShowValue(point);
        }

        return true;
    }
}
